

package buscar

class LoginController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	static String LastUrl;

	def index = {
		redirect(action: "list", params: params)
	}

	def beforeInterceptor = [action:this.&checkUser]
	def checkUser() {

		if(session.cliente ) {
			if(request.getRequestURL().toString().equals("http://localhost:8080/buscar/login/doLogout.dispatch"))
				this.doLogout();

			String a=request.getRequestURL().toString()
			if(request.getRequestURL().toString().equals("http://localhost:8080/buscar/grails/login/list.dispatch")){
				flash.message ="Usuario  ${session.cliente.nome} ja logado"

				redirect(url:request.getHeader("Referer"))
			}

			redirect(url:"http://localhost:8080/buscar")

			return false
		}
	}

	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[loginInstanceList: Login.list(params), loginInstanceTotal: Login.count()]
	}

	def create = {
		def loginInstance = new Login()
		loginInstance.properties = params
		return [loginInstance: loginInstance]
	}

	def save = {
		def loginInstance = new Login(params)
		if (loginInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'login.label', default: 'Login'), loginInstance.id])}"
			redirect(action: "show", id: loginInstance.id)
		}
		else {
			render(view: "create", model: [loginInstance: loginInstance])
		}
	}

	def show = {
		def loginInstance = Login.get(params.id)
		if (!loginInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'login.label', default: 'Login'), params.id])}"
			redirect(action: "list")
		}
		else {
			[loginInstance: loginInstance]
		}
	}

	def edit = {
		def loginInstance = Login.get(params.id)
		if (!loginInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'login.label', default: 'Login'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [loginInstance: loginInstance]
		}
	}

	def update = {
		def loginInstance = Login.get(params.id)
		if (loginInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (loginInstance.version > version) {

					loginInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'login.label', default: 'Login')]
					as Object[], "Another user has updated this Login while you were editing")
					render(view: "edit", model: [loginInstance: loginInstance])
					return
				}
			}
			loginInstance.properties = params
			if (!loginInstance.hasErrors() && loginInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'login.label', default: 'Login'), loginInstance.id])}"
				redirect(action: "show", id: loginInstance.id)
			}
			else {
				render(view: "edit", model: [loginInstance: loginInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'login.label', default: 'Login'), params.id])}"
			redirect(action: "list")
		}
	}

	def delete = {
		def loginInstance = Login.get(params.id)
		if (loginInstance) {
			try {
				loginInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'login.label', default: 'Login'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'login.label', default: 'Login'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'login.label', default: 'Login'), params.id])}"
			redirect(action: "list")
		}
	}

	def login = {
	}
	def doLogin = {
		def cliente = Cliente.findWhere(email:params['email'],
				password:params['password'])
		session.cliente = cliente

		if (cliente){
			LastUrl=request.getHeader("Referer")
			flash.message = "Bem vindo! ${session.cliente.nome}"
			redirect(url:"http://localhost:8080/buscar")
		}
		else{
			flash.message = "Verifique o seu login e senha\nTente Novamente"
			redirect(url:'http://localhost:8080/buscar/login/list')
		}
	}

	def doLogout = {

		if(!session.cliente) {
			flash.message = "Voce nao esta logado!"
			redirect(url:"http://localhost:8080/buscar")
		}else{
			flash.message = "Sessao encerrada de ${session.cliente.nome}"
			session.cliente = null
			redirect(url:"http://localhost:8080/buscar")
		}
	}
}
